
const { controller_save,
    controller_find, controller_delete,
    controller_get } = require("../../../controller/product/list/PRODUCT.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");
const pageSizeList = require("../../../utils/slicePageSize");

/**
 * @description 分类路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/product/list");

/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        const {        // shopId,
            id = undefined,
            cateId = undefined,
            brandId = undefined,
            intro = undefined,
            professionId = undefined,
            title = undefined,
            masterImages = undefined,
            price = undefined,
            sellPrice = undefined,
            shelfTime = undefined,
            description = undefined,
            sort = undefined,
            status = undefined,
            sku,
            specContent, //规格数据
            seoTitle,
            seoDescription,
            seoKeyWords,
            seoUrl,
            takeDown,
            snapshot,
        } = ctx.request.body
        const check = emptyCheck({ title, masterImages, price, sellPrice, description })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await controller_save({
                id, brandId,
                cateId, professionId, title,
                masterImages, intro, sku, specContent,
                price, sellPrice, shelfTime, description, sort, status,
                seoTitle,
                seoDescription,
                seoKeyWords,
                seoUrl,
                takeDown,
                snapshot,
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})


/**@description 根据id获取详情 */
router.get("/get", async (ctx, next) => {
    try {
        const { id = undefined, title = undefined } = ctx.query
        const res = await controller_get({ id, title });
        // console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除分类 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        // console.log("拿到id",ctx.query.id)
        const res = await controller_delete({ id: id });
        ctx.body = res;
    } catch (error) {
        console.log("删除出错", error)
        ctx.body = serverError
    }
})


// 查询列表
router.post("/getList", async (ctx, next) => {
    try {
        const { current, pageSize, title, status, classId, id } = ctx.request.body;
        const res = await controller_find({
            current, pageSize, title, status, classId, id
        })
        // const list = pageSizeList({ data: res, pageSize, current })
        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})



module.exports = router